/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBill;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class BusinessBillListForReceiveExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"BusinessBillListForReceive.xls\"");
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		c.setCellValue(new HSSFRichTextString("欠款合计："
				+ model.get("remainTotal")));
		sheet.addMergedRegion(new Region(0, 0, 0, 11));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("业务单号"));
		r.createCell(1).setCellValue(new HSSFRichTextString("订货日期"));
		r.createCell(2).setCellValue(new HSSFRichTextString("订货单位"));
		r.createCell(3).setCellValue(new HSSFRichTextString("摘要"));
		r.createCell(4).setCellValue(new HSSFRichTextString("结账日期"));
		r.createCell(5).setCellValue(new HSSFRichTextString("付款方式"));
		r.createCell(6).setCellValue(new HSSFRichTextString("总额"));
		r.createCell(7).setCellValue(new HSSFRichTextString("已收"));
		r.createCell(8).setCellValue(new HSSFRichTextString("调账"));
		r.createCell(9).setCellValue(new HSSFRichTextString("欠款"));
		r.createCell(10).setCellValue(new HSSFRichTextString("业务员"));
		r.createCell(11).setCellValue(new HSSFRichTextString("跟单员"));
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		List list = (List) model.get("list");
		List pmList = (List) model.get("pmList");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			BusinessBill b = (BusinessBill) objs[0];
			String estName = (String) objs[1];
			BigDecimal colCost = (BigDecimal) objs[2];
			BigDecimal adjCost = (BigDecimal) objs[3];
			String checkDate = "";
			if (b.getCheckDate() != null) {
				checkDate = sf.format(b.getCheckDate());
			}

			if (b.getLinkman() != null && !b.getLinkman().equals("")) {
				estName = estName + "(" + b.getLinkman() + ")";
			}

			BigDecimal remain = b.getCost().add(adjCost).subtract(colCost);
			r = sheet.createRow(os + 2);
			r.createCell(0).setCellValue(
					new HSSFRichTextString(b.getBillCode()));
			r.createCell(1).setCellValue(
					new HSSFRichTextString(sf.format(b.getOrderDate())));
			r.createCell(2).setCellValue(new HSSFRichTextString(estName));
			r.createCell(3)
					.setCellValue(new HSSFRichTextString(b.getContent()));
			r.createCell(4).setCellValue(new HSSFRichTextString(checkDate));
			r.createCell(5).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							pmList, b.getPayModeId())));
			r.createCell(6).setCellValue(b.getCost().doubleValue());
			r.createCell(7).setCellValue(colCost.doubleValue());
			r.createCell(8).setCellValue(adjCost.doubleValue());
			r.createCell(9).setCellValue(remain.doubleValue());
			r.createCell(10).setCellValue(
					new HSSFRichTextString(b.getManagerName()));
			r.createCell(11).setCellValue(
					new HSSFRichTextString(b.getFollowerNameStr()));
		}

		ServletOutputStream arg18 = response.getOutputStream();
		workbook.write(arg18);
		arg18.flush();
		arg18.close();
	}
}